invar_tab <- function(config = NULL, metric = NULL, scalar = NULL,
                      metric_partial_1 = NULL, metric_partial_2 = NULL,
                      scalar_partial_1 = NULL, scalar_partial_2 = NULL,
                      ordered = FALSE, intercepts = TRUE,
                      varnames = NULL, 
                      file_name = NULL, label = "", title = ""){
  
  ### TO WORK ON: Table notes
  
  # Specifying Models -----
  est_config <- parameterEstimates(config)
  est_metric <- parameterEstimates(metric)
  est_scalar <- parameterEstimates(scalar)
  
  models <- c("config", "metric", "scalar")
  
  if(!is.null(metric_partial_1)){
    est_metric_partial_1 <- parameterEstimates(metric_partial_1)
    models <- c(models, "metric_partial_1")
  }
  if(!is.null(metric_partial_2)){
    est_metric_partial_2 <- parameterEstimates(metric_partial_2)
    COLS <- COLS + 1
    models <- c(models, "metric_partial_2")
  }
  if(!is.null(scalar_partial_1)){
    est_scalar_partial_1 <- parameterEstimates(scalar_partial_1)
    models <- c(models, "scalar_partial_1")
  }
  
  if(!is.null(scalar_partial_2)){
    est_scalar_partial_2 <- parameterEstimates(scalar_partial_2)
    models <- c(models, "scalar_partial_2")
  }
  
  models <- sort(models)
  COLS <- length(models)
  
  # Creating table array
  # Intercepts Excluded ----
  if(intercepts == FALSE){
    loading_names <- NULL
    vars <- unique(est_config$rhs)[which(est_config$op == "=~")]
    vars <- vars[which(!is.na(vars))]
    
    k <- 1
    for(i in 1:length(vars)){
      loading_names[k] <- vars[i]
      loading_names[k + 1] <- paste("se", vars[i])
      k <- k + 2
    }
    fit_names <- c("$\\chi^2$", "DF", "CFI", "SRMR", "RMSEA [90\\% CI]", "N")
    
    items <- sum(length(c(loading_names, fit_names)))
    OUT_INVAR <- array(NA, c(items, COLS*2)) # 2 is number of groups
    rownames(OUT_INVAR) <- c(loading_names, fit_names)
    OUT_INVAR <- as.data.frame(OUT_INVAR)
    
    ## Enter Estimates (need to be able to loop over number of vars)
    j <- 1
    for(i in models){
      mod <- get(paste0("est_", i))
      # Group 1
      OUT_INVAR[loading_names[1], j] <- round(mod$est[which(mod$rhs == vars[1] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[2], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[1] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[3], j] <- round(mod$est[which(mod$rhs == vars[2] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[4], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[2] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[5], j] <- round(mod$est[which(mod$rhs == vars[3] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[6], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[3] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[7], j] <- round(mod$est[which(mod$rhs == vars[4] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[8], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[4] & mod$op == "=~")][1], 4), ")")
      
      # Group 2
      OUT_INVAR[loading_names[1], j+1] <- round(mod$est[which(mod$rhs == vars[1] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[2], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[1] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[3], j+1] <- round(mod$est[which(mod$rhs == vars[2] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[4], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[2] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[5], j+1] <- round(mod$est[which(mod$rhs == vars[3] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[6], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[3] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[7], j+1] <- round(mod$est[which(mod$rhs == vars[4] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[8], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[4] & mod$op == "=~")][2], 4), ")")
      mod <- get(i)
      OUT_INVAR["$\\chi^2$", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["chisq"]), "}")
      OUT_INVAR["DF", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["df"], 3), "}")
      OUT_INVAR["CFI", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["cfi"], 3), "}")
      OUT_INVAR["SRMR", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["srmr"], 4), "}")
      OUT_INVAR["RMSEA [90\\% CI]", j] <- paste0("\\multicolumn{2}{c}{",
                                                 round(fitMeasures(mod)["rmsea"], 4),
                                                 " [",
                                                 round(fitMeasures(mod)["rmsea.ci.lower"], 4),
                                                 ", ",
                                                 round(fitMeasures(mod)["rmsea.ci.upper"], 4),
                                                 "]}")
      OUT_INVAR["N", c(j, j+1)] <- c(mod@Data@nobs[[1]], mod@Data@nobs[[2]]) #paste0("\\multicolumn{2}{c}{", nobs(mod), "}")
      j <- j + 2
    }
    
    if(!is.null(varnames)){
      for(i in 1:length(vars)){
        rownames(OUT_INVAR) <- stringr::str_replace(rownames(OUT_INVAR), vars[i], varnames[i])
      }
      rownames(OUT_INVAR) <- stringr::str_replace(rownames(OUT_INVAR), "int", "Intercept")
    }  
  }
  
  # Intercepts included ----
  if(intercepts == TRUE){
    loading_names <- NULL
    intercept_names <- NULL
    vars <- unique(est_config$rhs)[which(est_config$op == "=~")]
    vars <- vars[which(!is.na(vars))]
    
    k <- 1
    for(i in 1:length(vars)){
      loading_names[k] <- vars[i]
      loading_names[k + 1] <- paste("se", vars[i])
      intercept_names[k] <- paste("int", vars[i])
      intercept_names[k + 1] <- paste("int se", vars[i])
      k <- k + 2
    }
    fit_names <- c("$\\chi^2$", "DF", "CFI", "SRMR", "RMSEA [90\\% CI]", "N")
    
    items <- sum(length(c(loading_names, intercept_names, fit_names)))
    OUT_INVAR <- array(NA, c(items, COLS*2)) # 2 is number of groups
    rownames(OUT_INVAR) <- c(loading_names, intercept_names, fit_names)
    OUT_INVAR <- as.data.frame(OUT_INVAR)
    
    ## Enter Estimates (need to be able to loop over number of vars)
    j <- 1
    for(i in models){
      mod <- get(paste0("est_", i))
      # Group 1
      OUT_INVAR[loading_names[1], j] <- round(mod$est[which(mod$rhs == vars[1] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[2], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[1] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[3], j] <- round(mod$est[which(mod$rhs == vars[2] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[4], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[2] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[5], j] <- round(mod$est[which(mod$rhs == vars[3] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[6], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[3] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[7], j] <- round(mod$est[which(mod$rhs == vars[4] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[8], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[4] & mod$op == "=~")][1], 4), ")")
      
      OUT_INVAR[intercept_names[1], j] <- round(mod$est[which(mod$lhs == vars[1] & mod$op == "~1")][1], 3)
      OUT_INVAR[intercept_names[2], j] <- paste0("(", round(mod$se[which(mod$lhs == vars[1] & mod$op == "~1")][1], 4), ")")
      OUT_INVAR[intercept_names[3], j] <- round(mod$est[which(mod$lhs == vars[2] & mod$op == "~1")][1], 3)
      OUT_INVAR[intercept_names[4], j] <- paste0("(", round(mod$se[which(mod$lhs == vars[2] & mod$op == "~1")][1], 4), ")")
      OUT_INVAR[intercept_names[5], j] <- round(mod$est[which(mod$lhs == vars[3] & mod$op == "~1")][1], 3)
      OUT_INVAR[intercept_names[6], j] <- paste0("(", round(mod$se[which(mod$lhs == vars[3] & mod$op == "~1")][1], 4), ")")
      OUT_INVAR[intercept_names[7], j] <- round(mod$est[which(mod$lhs == vars[4] & mod$op == "~1")][1], 3)
      OUT_INVAR[intercept_names[8], j] <- paste0("(", round(mod$se[which(mod$lhs == vars[4] & mod$op == "~1")][1], 4), ")")
      
      # Group 2
      OUT_INVAR[loading_names[1], j+1] <- round(mod$est[which(mod$rhs == vars[1] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[2], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[1] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[3], j+1] <- round(mod$est[which(mod$rhs == vars[2] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[4], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[2] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[5], j+1] <- round(mod$est[which(mod$rhs == vars[3] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[6], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[3] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[7], j+1] <- round(mod$est[which(mod$rhs == vars[4] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[8], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[4] & mod$op == "=~")][2], 4), ")")
      
      OUT_INVAR[intercept_names[1], j+1] <- round(mod$est[which(mod$lhs == vars[1] & mod$op == "~1")][2], 3)
      OUT_INVAR[intercept_names[2], j+1] <- paste0("(", round(mod$se[which(mod$lhs == vars[1] & mod$op == "~1")][2], 4), ")")
      OUT_INVAR[intercept_names[3], j+1] <- round(mod$est[which(mod$lhs == vars[2] & mod$op == "~1")][2], 3)
      OUT_INVAR[intercept_names[4], j+1] <- paste0("(", round(mod$se[which(mod$lhs == vars[2] & mod$op == "~1")][2], 4), ")")
      OUT_INVAR[intercept_names[5], j+1] <- round(mod$est[which(mod$lhs == vars[3] & mod$op == "~1")][2], 3)
      OUT_INVAR[intercept_names[6], j+1] <- paste0("(", round(mod$se[which(mod$lhs == vars[3] & mod$op == "~1")][2], 4), ")")
      OUT_INVAR[intercept_names[7], j+1] <- round(mod$est[which(mod$lhs == vars[4] & mod$op == "~1")][2], 3)
      OUT_INVAR[intercept_names[8], j+1] <- paste0("(", round(mod$se[which(mod$lhs == vars[4] & mod$op == "~1")][2], 4), ")")
      
      
      mod <- get(i)
      OUT_INVAR["$\\chi^2$", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["chisq"]), "}")
      OUT_INVAR["DF", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["df"], 3), "}")
      OUT_INVAR["CFI", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["cfi"], 3), "}")
      OUT_INVAR["SRMR", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["srmr"], 4), "}")
      OUT_INVAR["RMSEA [90\\% CI]", j] <- paste0("\\multicolumn{2}{c}{",
                                                 round(fitMeasures(mod)["rmsea"], 4),
                                                 " [",
                                                 round(fitMeasures(mod)["rmsea.ci.lower"], 4),
                                                 ", ",
                                                 round(fitMeasures(mod)["rmsea.ci.upper"], 4),
                                                 "]}")
      OUT_INVAR["N", c(j, j+1)] <- c(mod@Data@nobs[[1]], mod@Data@nobs[[2]]) #paste0("\\multicolumn{2}{c}{", nobs(mod), "}")
      j <- j + 2
    }
    
    if(!is.null(varnames)){
      for(i in 1:length(vars)){
        rownames(OUT_INVAR) <- stringr::str_replace(rownames(OUT_INVAR), vars[i], varnames[i])
      }
      rownames(OUT_INVAR) <- stringr::str_replace(rownames(OUT_INVAR), "int", "Intercept")
    }  
  }
  
  # Thresholds Included ----
  if(ordered == TRUE){
    loading_names <- NULL
    vars <- unique(est_config$rhs)[which(est_config$op == "=~")]
    vars <- vars[which(!is.na(vars))]
    
    n_thresh <- unique(est_config$rhs[which(est_config$op == "|")])
    threshold_names <- array(NA, c(length(vars)*2, length(n_thresh)))
    
    k <- 1
    for(i in 1:length(vars)){
      loading_names[k] <- vars[i]
      loading_names[k + 1] <- paste("se", vars[i])
      
      for(t in 1:length(n_thresh)){
        threshold_names[k, t] <- paste("thresh", vars[i], n_thresh[t])
        threshold_names[k + 1, t] <- paste("thresh se", vars[i], n_thresh[t]) 
      }
      k <- k + 2
    }
    threshold_names <- t(threshold_names)
    
    xx <- rep(NA, nrow(threshold_names)*ncol(threshold_names))
    t <- 1
    z <- 1
    n <- length(n_thresh)
    y <- seq(1, by = 2, length.out = length(vars))
    for(i in y){
      xx[t:n] <- threshold_names[, i]
      
      for(j in 1:nrow(threshold_names)){
        xx <- append(xx, threshold_names[j, i+1], after = z)
        z <- z + 2
      }
      
      t <- z
      n <- z + length(n_thresh) - 1
    }
    threshold_names <- xx[which(!is.na(xx))] # not sure why there is missingness
    rm(xx)
    
    fit_names <- c("$\\chi^2$", "DF", "CFI", "SRMR", "RMSEA [90\\% CI]", "N")
    
    
    items <- sum(length(c(loading_names, threshold_names, fit_names)))
    OUT_INVAR <- array(NA, c(items, COLS*2)) # 2 is number of groups
    rownames(OUT_INVAR) <- c(loading_names, threshold_names, fit_names)
    OUT_INVAR <- as.data.frame(OUT_INVAR)
    
    ## Enter Estimates (need to be able to loop over number of vars)
    j <- 1
    for(i in models){
      mod <- get(paste0("est_", i))
      
      # Group 1
      OUT_INVAR[loading_names[1], j] <- round(mod$est[which(mod$rhs == vars[1] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[2], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[1] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[3], j] <- round(mod$est[which(mod$rhs == vars[2] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[4], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[2] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[5], j] <- round(mod$est[which(mod$rhs == vars[3] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[6], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[3] & mod$op == "=~")][1], 4), ")")
      OUT_INVAR[loading_names[7], j] <- round(mod$est[which(mod$rhs == vars[4] & mod$op == "=~")][1], 3)
      OUT_INVAR[loading_names[8], j] <- paste0("(", round(mod$se[which(mod$rhs == vars[4] & mod$op == "=~")][1], 4), ")")
      
      t <- 1
      for(z in 1:length(vars)){
        it <- seq(t, by = 2, length.out = length(n_thresh))
        OUT_INVAR[threshold_names[it], j] <- round(mod$est[which(mod$lhs == vars[z] & mod$op == "|")][1:length(n_thresh)], 3)
        OUT_INVAR[threshold_names[it + 1], j] <- paste0("(", round(mod$se[which(mod$lhs == vars[z] & mod$op == "|")][1:length(n_thresh)], 4), ")") 
        t <- max(it) + 2
      }
      
      
      # Group 2
      OUT_INVAR[loading_names[1], j+1] <- round(mod$est[which(mod$rhs == vars[1] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[2], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[1] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[3], j+1] <- round(mod$est[which(mod$rhs == vars[2] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[4], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[2] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[5], j+1] <- round(mod$est[which(mod$rhs == vars[3] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[6], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[3] & mod$op == "=~")][2], 4), ")")
      OUT_INVAR[loading_names[7], j+1] <- round(mod$est[which(mod$rhs == vars[4] & mod$op == "=~")][2], 3)
      OUT_INVAR[loading_names[8], j+1] <- paste0("(", round(mod$se[which(mod$rhs == vars[4] & mod$op == "=~")][2], 4), ")")
      
      
      t <- 1
      for(z in 1:length(vars)){
        it <- seq(t, by = 2, length.out = length(n_thresh))
        OUT_INVAR[threshold_names[it], j + 1] <- round(mod$est[which(mod$lhs == vars[z] & mod$op == "|")][(1:length(n_thresh))+length(vars)], 3)
        OUT_INVAR[threshold_names[it + 1], j + 1] <- paste0("(", round(mod$se[which(mod$lhs == vars[z] & mod$op == "|")][(1:length(n_thresh))+length(vars)], 4), ")") 
        t <- max(it) + 2
      }
      
      mod <- get(i)
      # Do I want this flexible?
      OUT_INVAR["$\\chi^2$", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["chisq"]), "}")
      OUT_INVAR["DF", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["df"], 3), "}")
      OUT_INVAR["CFI", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["cfi"], 3), "}")
      OUT_INVAR["SRMR", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["srmr"], 4), "}")
      OUT_INVAR["RMSEA [90\\% CI]", j] <- paste0("\\multicolumn{2}{c}{",
                                                 round(fitMeasures(mod)["rmsea"], 4),
                                                 " [",
                                                 round(fitMeasures(mod)["rmsea.ci.lower"], 4),
                                                 ", ",
                                                 round(fitMeasures(mod)["rmsea.ci.upper"], 4),
                                                 "]}")
      OUT_INVAR["N", c(j, j+1)] <- c(mod@Data@nobs[[1]], mod@Data@nobs[[2]]) #paste0("\\multicolumn{2}{c}{", nobs(mod), "}") ## separate by group?
      
      if(ordered == TRUE){
        OUT_INVAR["$\\chi^2$", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["chisq.scaled"]), "}")
        OUT_INVAR["DF", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["df"], 3), "}")
        OUT_INVAR["CFI", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["cfi.scaled"], 3), "}")
        OUT_INVAR["SRMR", j] <- paste0("\\multicolumn{2}{c}{", round(fitMeasures(mod)["srmr"], 4), "}")
        OUT_INVAR["RMSEA [90\\% CI]", j] <- paste0("\\multicolumn{2}{c}{",
                                                   round(fitMeasures(mod)["rmsea.scaled"], 4),
                                                   " [",
                                                   round(fitMeasures(mod)["rmsea.ci.lower.scaled"], 4),
                                                   ", ",
                                                   round(fitMeasures(mod)["rmsea.ci.upper.scaled"], 4),
                                                   "]}")
        OUT_INVAR["N", c(j, j+1)] <- c(mod@Data@nobs[[1]], mod@Data@nobs[[2]]) #paste0("\\multicolumn{2}{c}{", nobs(mod), "}")
      }
      
      j <- j + 2
    }
    
    if(!is.null(varnames)){
      for(i in 1:length(vars)){
        rownames(OUT_INVAR) <- stringr::str_replace(rownames(OUT_INVAR), vars[i], varnames[i])
      }
      rownames(OUT_INVAR) <- stringr::str_replace(rownames(OUT_INVAR), "int", "Intercept")
    }  
  }
  
  # Creating table to print ----
  tab <- xtable(OUT_INVAR, 
                label = label,  # label
                align = c("l", rep("c", ncol(OUT_INVAR))),
                caption = title)
  
  model_names <- c("Configural", "Metric", "Scalar")
  if("metric_partial_1" %in% models){
    met_index <- which(model_names == "Metric")
    model_names <- append(model_names, "Metric--Partial", after = met_index)
  }
  if("metric_partial_2" %in% models){
    met_index <- which(model_names == "Metric")
    model_names <- append(model_names, "Metric--Partial2", after = met_index + 1)
  }
  if("scalar_partial_1" %in% models){
    scal_index <- which(model_names == "Scalar")
    model_names <- append(model_names, "Scalar--Partial", after = scal_index)
  }
  if("scalar_partial_2" %in% models){
    scal_index <- which(model_names == "Scalar")
    model_names <- append(model_names, "Scalar--Partial2", after = scal_index + 1)
  }
  
  # Colnames
  addtorow <- list()
  addtorow$pos <- list(0)
  addtorow$command[1] <- paste0(paste0('& \\multicolumn{2}{c}{', model_names, '}', collapse=''), '\\\\')
  
  # Model Note
  addtorow$pos[[2]] <- nrow(OUT_INVAR)
  addtorow$command[2] <- c(paste0("\\bottomrule \\multicolumn{", 
                                  (ncol(OUT_INVAR) + 1),
                                  "}{l}{\\multirow{2}{\\textwidth}{Note: Models estimated using maximum likelihood. Parameter estimates with standard errors in parentheses. Error covariance between \\textit{try hard} and \\textit{special favors} estimated but omitted. 2016 ANES}}"))
  
  
  if(!is.null(file_name)){
    print(tab,
          caption.placement = "top",
          sanitize.text.function = function(str) gsub("$\backslash$", "\\", str, fixed = TRUE),
          comment = F,
          add.to.row = addtorow,
          include.colnames = F,
          hline.after = c(-1, 0),
          file = file_name) 
  }
  if(is.null(file_name)){
    print(tab,
          caption.placement = "top",
          sanitize.text.function = function(str) gsub("$\backslash$", "\\", str, fixed = TRUE),
          comment = F,
          add.to.row = NOTE,
          hline.after = c(-1, 0))
  }
  
}